﻿using FPC.Offer.Model;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FPC.Offer.Repository
{
    public class System_RoleRightRepository : BaseRepository<Model.System_RoleRight>, IRepository.ISystem_RoleRightRepository
    {
        /// <summary>
        /// 删除角色权限
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public dynamic Role_PowerDelete(int id)
        {
            return DbContext.Database.ExecuteSqlCommand("DELETE FROM [FPC_Offer].[dbo].[System_RoleRight] WHERE RoleId=" + id);
        }
        /// <summary>
        /// 角色权限修改
        /// </summary>
        /// <param name="Roleid"></param>
        /// <param name="Powerid"></param>
        /// <returns></returns>
        public dynamic RoleBatchUpdate(int Roleid, string Powerid)
        {

            //var DBsr = DbContext.Set<System_RoleRight>();
            //var ss = DBsr.Find();
            //DBsr.Remove(ss);

            //foreach (var v in Powerid.Split(','))
            //{
            //    int p = int.Parse(v);
            //    DBsr.Add(new System_RoleRight() { RoleId = Roleid, PowerId = p });
            //}
            //return DbContext.SaveChanges();

            //创建CMD             
            var cmd = DbContext.Database.Connection.CreateCommand();
            cmd.Connection.Open();
            cmd.Transaction = cmd.Connection.BeginTransaction();
            var trans = cmd.Transaction;
            //用来接收执行后的结果
            int GetCount = 0;
            try
            {
                //先删除 所有权限
                string Delsql = "DELETE FROM [FPC_Offer].[dbo].[System_RoleRight] WHERE RoleId= " + Roleid;
                cmd.CommandText = Delsql;
                cmd.ExecuteNonQuery();



                //在添加新的权限
                var ss = Powerid.Split(',');
                string Addsql = " INSERT INTO [FPC_Offer].[dbo].[System_RoleRight] values";

                //循环把权限Id取出来，用于添加
                for (int i = 0; i < ss.Length; i++)
                {
                    //权限ID
                    var PowerId = Convert.ToInt32(ss[i]);
                    //添加角色权限
                    Addsql += "(" + Roleid + "," + PowerId + "),"; 
                };
                Addsql.TrimEnd(',');
                var sql = Addsql.Remove(Addsql.Length - 1, 1);
                //获取结果
                cmd.CommandText = sql;
                cmd.ExecuteNonQuery();
                trans.Commit();
            }
            catch (Exception ee)
            {
                trans.Rollback();
                string ss = ee.Message;
                return ss;
            }
            return 1;
        }
        /// <summary>
        /// 测试是否有该权限
        /// </summary>
        /// <param name="Roleid"></param> 
        /// <param name="Url"></param>
        /// <returns></returns>
        public dynamic GetIsRoleRight(string Roleid, string Url, ref int Modid)
        {
            Modid = 0;
            var id = Convert.ToInt32(Roleid);
            var db = DbContext.Set<Model.System_RoleRight>();
            var Rolerightdb = db.Where(t => t.RoleId == id).ToList();
            var powerdb = DbContext.Set<Model.System_PowerTable>();
            foreach (var item in Rolerightdb)
            {
                if (powerdb.Where(t => t.PowerId == item.PowerId && t.PowerAddress.Contains(Url)).FirstOrDefault() != null)
                {
                    Modid = powerdb.Where(t => t.PowerId == item.PowerId && t.PowerAddress.Contains(Url)).FirstOrDefault().PowerId;
                }              
            }
            if (Modid != 0)
            {
                return true;
            }
            else 
            {
                return false;
            }
        }
        /// <summary>
        /// 测试是否有该权限
        /// </summary>
        /// <param name="Roleid"></param> 
        /// <param name="Url"></param>
        /// <returns></returns>
        public dynamic GetIsRoleRight(string RoleID, int MenuID)
        {
            var db = DbContext.Set<Model.System_RoleRight>();
            var roleid = Convert.ToInt32(RoleID);
            if (db.Where(t => t.RoleId == roleid && t.PowerId == MenuID) != null)
            {
                return true;
            }
            else {
                return false;
            }                   
        }
    }
}
